#ifndef _DIRINIT_H_
#define _DIRINIT_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <string>

#define INFOS stderr	/* stream for outputting diagnostic messages */

#define tagfsDirName ".tagfs"
#define filesDirName "Files"

using namespace std;

class DirInit {

    private:
	/* key directories */
	char* tagfsDir;
	char* mountDir;
	char* repoDir;
	char* filesDir;
	char* currDir;
	
	int verbose;
	int build;
	string build_dir;

	/* clean temporary structures and do exit(ret) */
	void cleanup(int ret);
	/* output error message m to stream str */
	void descError(FILE* str, const char* m);
	/* allocate memory and check for errors */
	void* doMalloc(int size);
	/* checks for existence of a directory and creates it; 0 on success */ 
	int enterDir(char* dir);
	
	/*
 	* rel2abs: convert an relative path name into absolute.
 	*
 	*	i)	path	relative path
 	*	i)	base	base directory (must be absolute path)
 	*	o)	result	result buffer
 	*	i)	size	size of result buffer
 	*	r)		!= NULL: absolute path
 	*			== NULL: error
 	*/
	char* rel2abs(const char *path, const char *base,
		char *result, const size_t size);

    public:
	/* main function testing parameters and setting up dirs */
	void initDirs(int argc, char** argv);

	/* returns mountpoint; prerequisite: initDirs */
	char* getMountDir();

	/* return repository directory; prereq: initDirs */
	char* getRepoDir();

	/* return Files directory inside repo's dir; prereq: initDirs */
	char* getFilesDir();
	
	int isVerbose();
	
	int shouldBuild();
	
	string getBuildDir();
	
	DirInit();
};

#endif
